package com.jml.service.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import lombok.extern.slf4j.Slf4j;


@Slf4j
@Component
public class SentinelApplicationRunner implements ApplicationRunner {

    // 定义的限流规则名称
    private static final String GETORDER_KEY = "orderToMember";

     /*这段代码表示项目启动之后就会调用这个方法加载限流配置，
     如果不这样写，可以写在普通的controller然后通过url调用来调用下面的代码加载限流配置

     如果配置这个类，就不会主动去nacos获取流控配置，相当于我们自己配置的就会覆盖了nacos获取的配置

     */
    //创建限流规则
    @Override
    public void run(ApplicationArguments args) throws Exception {
        /*List<FlowRule> rules = new ArrayList<FlowRule>();
        FlowRule rule1 = new FlowRule();
        //设置资源名称
        rule1.setResource(GETORDER_KEY);
        // QPS控制在1    ---qps表示每秒访问数
        rule1.setCount(1);
        // QPS限流
        rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
        //限流应用名称是默认的
        rule1.setLimitApp("default");
        rules.add(rule1);
        FlowRuleManager.loadRules(rules);
        log.info(">>>限流配置加载成功<<<");*/
    }
}
